// -*- coding: utf-8 -*- 
/**
 * Project: Java8InActionT
 * Creator: yanking
 * Create time: 2022-01-30 10:00
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.measurePerf;

import java.util.stream.LongStream;

public class accumulator {
    private long total = 0L;

    // sequential
    public static long sequentialAcc(long n) {
        return LongStream.rangeClosed(1, n).reduce(0L, Long::sum);
    }

    // Parallel
    public static long parallelAcc(long n) {
        return LongStream.rangeClosed(1, n)
                .parallel().
                        reduce(0L, Long::sum);
    }

    // foreach block error
    public static long forEachSum(long n) {
        accumulator accumulator = new accumulator();
        LongStream.rangeClosed(1, n).parallel().forEach(accumulator::add);
        return accumulator.total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public void add(long n) {
        total += n;
    }

}
